package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: thoughtworker
 * Date: 10/14/13
 * Time: 11:06 AM
 * To change this template use File | Settings | File Templates.
 */
public class Main {

    public static void main(String[] args) throws IOException {
        BibliotecaApp bibliotecaApp = new BibliotecaApp(new BufferedReader(new InputStreamReader(System.in)),
                System.out, new ValidateInMap(setUpInitialMenuMap()));
        bibliotecaApp.printWelcomeMessage();
        bibliotecaApp.startApp();
    }

    private static Map<String, Command> setUpInitialMenuMap() {
        List<String> guestMenuOptions = Arrays.asList("Enter 3 to view all books.", "Enter 4 to view all movies.");
        List<String> userMenuOptions = Arrays.asList("Enter 3 to view all books.", "Enter 4 to view all movies.", "Enter 5 to reserve a book.", "Enter 6 to check your account.");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        Menu guestMenu = new Menu(System.out, reader, guestMenuOptions);
        Menu userMenu = new Menu(System.out, reader, userMenuOptions);
        Map<String, Command> initialMenuMap = new HashMap<String, Command>();
        initialMenuMap.put("1", new LogInAsGuestCommand(reader, guestMenu, new ValidateInMap(setUpGuestMenuMap())));
        initialMenuMap.put("2", new LogInAsUserCommand(reader, new ValidateUser(setUpAllUsers()), userMenu, new ValidateInMap(setUpUserMenuMap())));
        initialMenuMap.put("Invalid", new InvalidOptionsCommand());
        return initialMenuMap;
    }

    private static Map<String, Command> setUpGuestMenuMap() {
        Map<String, Command> guestMenuMap = new HashMap<String, Command>();
        guestMenuMap.put("3", new PrintBooksCommand(System.out, new LibraryCatalogue(System.out)));
        guestMenuMap.put("4", new PrintMoviesCommand());
        guestMenuMap.put("Invalid", new InvalidOptionsCommand());
        return guestMenuMap;
    }

    private static Map<String, Command> setUpUserMenuMap() {
        Map<String, Command> userMenuMap = new HashMap<String, Command>();
        userMenuMap.put("3", new PrintBooksCommand(System.out, new LibraryCatalogue(System.out)));
        userMenuMap.put("4", new PrintMoviesCommand());
        userMenuMap.put("5", new ReserveBookCommand());
        userMenuMap.put("6", new DisplayMembershipDetailsCommand());
        userMenuMap.put("Invalid", new InvalidOptionsCommand());
        return userMenuMap;
    }

    private static Map<String, String> setUpAllUsers() {
        Map<String, String> users = new HashMap<String, String>();
        users.put("user1", "password1");
        users.put("user2", "password2");
        users.put("invalid", "invalid");
        return users;
    }
}
